package com.syq.service.impl;

import com.syq.config.IdWorker;
import com.syq.constant.RabbitMQConstants;
import com.syq.constant.ReportStateConstants;
import com.syq.constant.SourceType;
import com.syq.form.SmsInterfaceForm;
import com.syq.model.StandardSubmit;
import com.syq.service.InterfaceFormService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Set;

/**
 * @ClassName: InterfaceFormServiceImpl
 * @Description: TODO
 * @Create by: syq
 * @Date: 2022/8/24 16:54
 */

@Service
public class InterfaceFormServiceImpl implements InterfaceFormService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private IdWorker idWorker;

    @Override
    public void submitInterfaceForm(SmsInterfaceForm smsInterfaceForm) {
        String mobile = smsInterfaceForm.getMobile();
        String[] mobiles = mobile.split(",");
        if (!StringUtils.isEmpty(mobiles)){
            for (String s : mobiles) {
                StandardSubmit standardSubmit=new StandardSubmit();
                standardSubmit.setSequenceId(String.valueOf(idWorker.nextId()));
                standardSubmit.setClientID(Integer.parseInt(smsInterfaceForm.getClientID()));
                standardSubmit.setSrcSequenceId(Long.parseLong(smsInterfaceForm.getSrcID()));
                standardSubmit.setDestMobile(s);
                standardSubmit.setMessageContent(smsInterfaceForm.getContent());
                standardSubmit.setReportState(ReportStateConstants.WAIT);
                standardSubmit.setSendTime(new Date());
                standardSubmit.setSource(SourceType.HTTP);
                rabbitTemplate.convertAndSend(RabbitMQConstants.TOPIC_PRE_SEND+"_exchange", "",standardSubmit);
            }
        }
    }
}
